package service

import (
	"apitestAfterGo/dao"
	"apitestAfterGo/domain"
	"apitestAfterGo/dto"
)

type taskReportService struct {
	GetReportsByRecordId func(dto dto.TaskReportDto) ([]domain.TaskReport, int)
	GetReportByParentId  func(id int) []domain.TaskReport
}

var TaskReportServiceImpl taskReportService

func init() {
	TaskReportServiceImpl = taskReportService{
		GetReportsByRecordId: func(dto dto.TaskReportDto) ([]domain.TaskReport, int) {
			var taskReports []domain.TaskReport
			dto.Current = (dto.Current - 1) * dto.PageSize
			dao.GormDb.Debug().Where("task_record_id = ? and result like ? and parent_id = 0", dto.TaskRecordId, "%"+dto.Result+"%").Limit(dto.PageSize).Offset(dto.Current).Find(&taskReports)
			for i := range taskReports {
				taskReports[i].Children = TaskReportServiceImpl.GetReportByParentId(int(taskReports[i].ID))
			}
			var count int64
			dao.GormDb.Debug().Model(&domain.TaskReport{}).Select("count(1)").Where("task_record_id = ? and result like ? and parent_id = 0", dto.TaskRecordId, "%"+dto.Result+"%").Count(&count)
			return taskReports, int(count)
		},
		GetReportByParentId: func(id int) []domain.TaskReport {
			var taskReports []domain.TaskReport
			dao.GormDb.Debug().Where("parent_id = ?", id).Find(&taskReports)
			return taskReports
		},
	}
}
